package com.hc.model.code02.test5;

import java.io.Serializable;

/**
 * @version v1.0
 * @ClassName: Singleton
 * @Description: 静态内部类实现
 * @Author: HC
 */
public class Singleton implements Serializable {

    private static boolean flag = false;

    // 私有化构造
    private Singleton() {
        synchronized (Singleton.class) {
            // 解决反射导致破坏单例对象
            if (flag) {
                throw new RuntimeException("不能创建多个对象");
            }
            flag = true;
        }
    }

    // 创建静态内部类
    public static class SingletonHolder {
        // 构造对象
        private final static Singleton INSTANCE = new Singleton();
    }

    public static Singleton getInstance() {
        return SingletonHolder.INSTANCE;
    }

    public Object readResolve() {
        return SingletonHolder.INSTANCE;
    }
}
